'use strict';

angular.module('xuexibangApp')
	.controller('TeacherCtrl', function($scope, $http, Util, Auth, User, Klass) {

		// Use the User $resource to fetch all users
		$scope.courses = [];
		$scope.klasses = [];
		$scope._id = "";
		$scope.password="";
		$scope.users = User.teachers(
			function() {
				//console.log($scope.users.courses);
				for (var i = 0; i < Util.courses.length; i++) {
					//console.log(Util.courses[i]);
					$scope.courses.push({
						checked: false,
						code: Util.courses[i],
						name: Util.courses[i]
					});
				}
				Klass.query(function(klasses) {
					for (var i = 0; i < klasses.length; i++) {
						$scope.klasses.push({
							checked: false,
							code: klasses[i].code,
							name: klasses[i].name
						});
					}
				});
			}
		);
		$scope.edit = function(user) {
			nullinput();
			$scope._id = user._id;
			$scope.code = user.code;
			$scope.name = user.name;
			if (user.chargeklass) {
				$scope.isHeadTeacher = true;
				$scope.chargeklass = user.chargeklass;
			}
			if (user.courses && user.courses.length > 0)
				var usercourses = user.courses.join();
			for (var i = 0; i < $scope.courses.length; i++) {
				if (usercourses && usercourses.indexOf($scope.courses[i].code) > -1) {
					$scope.courses[i].checked = true;
				}
			}
			if (user.klasses && user.klasses.length) {
				var userklasses = user.klasses.join();
				for (var i = 0; i < $scope.klasses.length; i++) {
					if (userklasses && userklasses.indexOf($scope.klasses[i].code) > -1) {
						$scope.klasses[i].checked = true;
					}
				}
			}

			$scope.submitted = false;
		}
		$scope.save = function(form) {
			$scope.submitted = true;
			if (form.$valid) {
				var user = {
					code: $scope.code,
					name: $scope.name,
					courses: Util.getChecked($scope.courses),
					klasses: Util.getChecked($scope.klasses),
					chargeklass: $scope.chargeklass,
					role: ['teacher'],
					setpass: $scope.setpass,
					password: $scope.password
				}
				if ($scope._id != "") {
					user._id = $scope._id;
				};
				User.save(user, function(data) {
					var user = data;
					console.log(user);
					if ($scope._id === "") {
						$scope.users.push(user);
					} else {
						for (var i = 0; i < $scope.users.length; i++) {
							if ($scope.users[i]._id === user._id) {
								$scope.users[i] = user;
								break;
							}
						}
					}
					nullinput();
				}, function(err) {
					err = err.data;
					//if(err.errors.code)
					$scope.errors = {};
					// Update validity of form fields that match the mongoose errors
					angular.forEach(err.errors, function(error, field) {
						console.log(error, field);
						form[field].$setValidity('mongoose', false);
						$scope.errors[field] = error.message;
					});
					console.log(err);
				});
			};
		}

		$scope.reset = nullinput;
		function nullinput() {
			$scope._id = $scope.code = $scope.name = "";
			$scope.isHeadTeacher = false;
			for (var i = 0; i < $scope.courses.length; i++) {
				$scope.courses[i].checked = false;
			}
			for (var i = 0; i < $scope.klasses.length; i++) {
				$scope.klasses[i].checked = false;
			}
			$scope.chargeklass = "";
			$scope.setpass = false;
			$scope.password = "";
			$scope.submitted = false;
		}
		$scope.delete = function(user) {
			User.remove({
				id: user._id
			},function(data){
				//console.log(data);
				angular.forEach($scope.users, function(u, i) {
					if (u === user) {
						$scope.users.splice(i, 1);
					}
				});
			},function(err){
				console.log(err);
			});
		};
	})
